<ul class="breadcrumb">
    <li><a href="/com/mods/list">我的Mod列表</a></li>
    <li><a href="/com/mods/list">{$modname}</a></li>
    <li><a href="/com/mods/desclist/{$modid}">{$descname}</a></li>
    <li class="active">添加文件</li>
</ul>

<form action="/com/mods/addfile/{$descid}?do=submit" method="POST" enctype="multipart/form-data">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">适用游戏版本(多个用|隔开)</h3>
        </div>
        <div class="panel-body">
            <input type="text" class="form-control" name="appver" placeholder="2.2.10.4">
        </div>
    </div>
    
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">适用API版本</h3>
        </div>
        <div class="panel-body">
            <div class="dropdown">{$vhtml}</div>
        </div>
    </div>
    
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">选择文件</h3>
        </div>
        <div class="panel-body">
            <div class="progress" style="display:none" id="prshow"><div class="progress-bar progress-bar-success" role="progressbar" id="pr"></div></div>
            <div id="upshow">
            <button id="test1" class="btn btn-sm btn-info" type="button">上传文件</button>        
            <input type="hidden" id="faddr" name="faddr">
            <input type="hidden" id="fname" name="fname">
            <input type="hidden" id="ftype" name="ftype">
            <input type="hidden" id="vid" name="vid">
            </div>
            <div id="sucshow"></div>
        </div>
    </div>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">Submit</h3>
        </div>
        <div class="panel-body">
            <input type="submit" class="btn btn-primary" value="保存">
        </div>
    </div>
</form>

<script>
  layui.use('upload', function(){
  var $ = layui.jquery
  ,upload = layui.upload;
  var UpPage=0;
  var UpStatus=0;
  var UpTotalPage=0;
  var UpTotalLen=0;
  var uploadInst = upload.render({
    elem: '#test1'
    ,url: '/com/modupload'
    ,accept:'file',
    auto: false,
    choose: function (obj) {
	    $("#prshow").css("display","block");
		var data = this.data;
		var files = obj.pushFile();
		var LENGTH = 1000 * 1024; //每片文件大小
		obj.preview(function (index, file, result) {
			var totalSize = file.size;
			UpTotalLen=totalSize;
			UpTotalPage = Math.ceil(totalSize / LENGTH);
			UpPage=1;
			UpStatus=1;
			var fileName = file.name;
			var fileExt = fileName.substr(fileName.lastIndexOf('.') + 1);
			fileName = fileName.substr(0, fileName.lastIndexOf('.'));
			var progressTimer = setInterval(function () {
				var totalPage = UpTotalPage;
				var page = UpPage;
				if (UpTotalPage == UpPage && (UpStatus == 2 || UpStatus == -1)) {
					clearInterval(progressTimer);
				} else {
					if (UpStatus == 1) {
						UpStatus=0;
						data.fileName = fileName;
						data.startlen = (page - 1) * LENGTH;
						data.endlen = Math.min(page * LENGTH, UpTotalLen);
						data.totallen = UpTotalLen;
						data.fileExt = fileExt;
						obj.upload(index, file.slice((page - 1) * LENGTH, page * LENGTH));
					}
				}
			}, 100);
		});
	},
	done: function (res) {
		if (res.code == 1) { //分片上传
			UpPage = UpPage + 1;
			UpStatus=1;
			$("#pr").css("width",Math.ceil(UpPage * 100 / UpTotalPage) + '%');
			console.log(UpPage);
		} else if (res.code == 2) { //上传完成
                $("#faddr").val(res.url);
                $("#fname").val(res.name);
                $("#ftype").val(res.type);
                $("#vid").val(res.type);
                $("#upshow").css("display","none");
                $("#sucshow").css("display","block");
                $("#sucshow").html("已上传文件");
                layer.msg('上传成功', {time: 1000, anim: 0}, function () {});
		} else { //上传错误
    			console.log(!typeof (res.downUrl) == "undefined");
    			if (typeof (res.downUrl) == "undefined") {}
    			$("#prshow").css("display","none");
    			layer.msg("上传失败，"+res.msg, {time: 3000, anim: 0}, function () { });
		}
	},
	error: function(){
	}
  });
    });
</script>    
    